package com.guochaojava.websocket;

import cn.hutool.core.lang.Console;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.servlet.http.HttpSession;
import java.util.Map;


/**
 * @author guochao
 * @since 1.0.0
 */
public class WebSocketInterceptor implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;

            HttpSession session = serverHttpRequest.getServletRequest().getSession();
         /*   Map parameterMap = serverHttpRequest.getServletRequest().getParameterMap();
            Console.log(parameterMap);*/
            String sid = ((ServletServerHttpRequest) request).getServletRequest().getParameter("info");
            Console.log(sid);
            if (session != null) {
                map.put("info", sid);
            }

        }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }
}